package com.vinjogames.entities;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.PointF;

import com.vinjogames.gfx.Image;
import com.vinjogames.gfx.Shapes;
import com.vinjogames.util.GameConVars;
import com.vinjogames.util.LineSegmentF;
import com.vinjogames.util.PointFFlagged;

public abstract class GameObject {
	
	public static int ATTACKOBJ = 1;
	public static int BASE = 2;
	public static int BENIGN = 3;
	
	protected boolean isActive;
	protected Image mImage;
	protected float mGLWidth = GameConVars.UNIT_DEFAULT_WIDTH;
	protected float mGLHeight = GameConVars.UNIT_DEFAULT_HEIGHT;
	
	protected float hitRadius;
	protected PointF mLocation;
	protected PointF botLeft;
	protected LineSegmentF[] collisionLines;
	protected int objectType;
	protected float health;
	protected float maxHealth;
	protected float armor;
	protected int currentState;
	
	PointF mBarBL;
	PointF mBarTR;
	float mEmptyBar[];
	float mFullBar[];
	float barWidth = 0.1f;	
	
	public GameObject() {
		mImage = null;
		hitRadius = Math.max(mGLWidth, mGLHeight) * 0.5f;
		mLocation = new PointF(0,0);
		health = 1;
		armor = 0;
		currentState = 0;
		isActive = true;
		
        mBarBL = new PointF();
        mBarTR = new PointF();
        mFullBar   = new float[12];
        mEmptyBar  = new float[12];
        objectType = GameConVars.OBJECT_MISC;
	}
	
	public GameObject(int type) {
		this();
		objectType = type;
	}
	
	public void draw(GL10 gl) {
		mImage.draw(gl, mLocation, currentState);
	}
	
	public void draw(GL10 gl, PointF location, float r, float g, float b, float a){
		mImage.draw(gl, location.x, location.y, r, g, b, a);
	}
	
	public void drawBars(GL10 gl, float x, float y, float glWidth, float glHeight, float percentFull, float r, float g, float b, float a){
		mBarBL.set(x-glWidth/2, y - glHeight/2-barWidth);//Bottom left
		mBarTR.set(mBarBL.x+glWidth, mBarBL.y + barWidth);//Top right

		mEmptyBar[0] = mBarBL.x;
		mEmptyBar[1] = mBarBL.y;
		mEmptyBar[2] = 0;
		mEmptyBar[3] = mBarBL.x;
		mEmptyBar[4] = mBarTR.y;
		mEmptyBar[5] = 0;
		mEmptyBar[6] = mBarTR.x;
		mEmptyBar[7] = mBarBL.y;
		mEmptyBar[8] = 0;
		mEmptyBar[9] = mBarTR.x;
		mEmptyBar[10] = mBarTR.y;
		mEmptyBar[11] = 0;

		mFullBar[0] = mBarBL.x;
		mFullBar[1] = mBarBL.y;
		mFullBar[2] = 0;
		mFullBar[3] = mBarBL.x;
		mFullBar[4] = mBarTR.y;
		mFullBar[5] = 0;
		mFullBar[6] = mBarTR.x-((1f-percentFull)*glWidth);
		mFullBar[7] = mBarBL.y;
		mFullBar[8] = 0;
		mFullBar[9] = mBarTR.x-((1f-percentFull)*glWidth);
		mFullBar[10] = mBarTR.y;
		mFullBar[11] = 0;

		Shapes.drawSquare(gl, mEmptyBar, 1, 0, 0, 1);//default empty color
		Shapes.drawSquare(gl, mFullBar, r, g, b, a);
	}
	
	public void drawCollisionBox(GL10 gl){
		LineSegmentF[] lines = this.getBasicCollisionLines();
		if(lines != null){
			mEmptyBar[0] = lines[3].getStart().x;
			mEmptyBar[1] = lines[3].getStart().y;
			mEmptyBar[2] = 0;
			mEmptyBar[3] = lines[0].getStart().x;
			mEmptyBar[4] = lines[0].getStart().y;
			mEmptyBar[5] = 0;
			mEmptyBar[6] = lines[2].getStart().x;
			mEmptyBar[7] = lines[2].getStart().y;
			mEmptyBar[8] = 0;
			mEmptyBar[9] = lines[1].getStart().x;
			mEmptyBar[10] = lines[1].getStart().y;
			mEmptyBar[11] = 0;

			Shapes.drawSquare(gl, mEmptyBar, 1, 0, 0, .5f);
		}
	}
	
	public void rotate(float angle){ mImage.rotate(angle); }
	
	public boolean takeDamage(float dmg) {
		health -= dmg;
		return (health <= 0.0f);
	}
	
	public LineSegmentF[] getBasicCollisionLines() {
		PointFFlagged tl = new PointFFlagged(mLocation.x - 0.5f * mGLWidth, mLocation.y + mGLHeight);
		PointFFlagged tr = new PointFFlagged(mLocation.x + 0.5f * mGLWidth, mLocation.y + mGLHeight);
		PointFFlagged bl = new PointFFlagged(mLocation.x - 0.5f * mGLWidth, mLocation.y);
		PointFFlagged br = new PointFFlagged(mLocation.x + 0.5f * mGLWidth, mLocation.y);
		if(collisionLines == null) collisionLines = new LineSegmentF[4];
		collisionLines[0] = new LineSegmentF(tl, tr);
		collisionLines[1] = new LineSegmentF(tr, br);
		collisionLines[2] = new LineSegmentF(br, bl);
		collisionLines[3] = new LineSegmentF(bl, tl);
		return collisionLines;		
	}
	
	public boolean insideBox(PointF p) {
		return p.x >= (mLocation.x - 0.5f * mGLWidth) && p.y >= mLocation.y & p.x <= mLocation.x + 0.5f * mGLWidth && p.y <= mLocation.y + mGLHeight;
	}	
	
	public abstract void clearInfo();
	
	//Getters and setters.
	public PointF getLocation() { return new PointF(mLocation.x, mLocation.y); }
	public void setLocation(PointF l) {mLocation.x = l.x; mLocation.y = l.y;}
	public void setX(float x) {mLocation.x = x;}
	public void setY(float y) {mLocation.y = y;}
	public Image getImage() {return mImage;}
	public float getHitRadius() {return hitRadius;}
	public LineSegmentF[] getCollisionLines() {return collisionLines;}
	public float getGLWidth(){ return mGLWidth; }
	public float getGLHeight(){ return mGLHeight; }
	public float getHealth() { return health; }
	public void setHealth(float health) { this.health = health;	}
	public void activateObject() { this.isActive = true; }
	public void deactivateObject() { this.isActive = false; }
	public boolean checkActiveStatus() { return this.isActive; }
	public int getType() { return objectType; }
	public int getCurrentState() { return currentState; }
	public void setCurrentState(int state) { currentState = state; }
}
